#include "io_flash.h"
#include "SST25VF016B.h"


static struct flash_driver sst25v016b_driver = {
    SPI_FLASH_Init,
    SPI_FLASH_Test,
    SSTF016B_Erase,
    SSTF016B_WR,
    SSTF016B_RD
};

bool io_flash_open(struct flash_device *device)
{
    uint32_t temp;
    
    if(device->chip_id == 0x00BF2541)
    {
        device->driver = &sst25v016b_driver;
    }

    device->driver->init();
    temp = device->driver->open();
    if(temp != device->chip_id)
        return FALSE;
    else
        return TRUE;
}

uint32_t io_flash_write(struct flash_device *device, uint32_t addr, 
                    uint8_t *pdata, uint32_t len)
{
    uint32_t sec1;
    sec1 = addr / SEC_SIZE;
    if((addr % SEC_SIZE) == 0) 
        device->driver->erase(sec1, sec1);
    return device->driver->write(addr, pdata, len);
}

uint32_t io_flash_read(struct flash_device *device, uint32_t addr,
                    uint8_t *pdata, uint32_t len)
{
    return device->driver->read(addr, pdata, len);
}



